<?php

namespace App\Models;

use App\Http\Common\Constant;
use App\Http\Common\Proportion;
use App\Http\Services\NotifyService;
use Illuminate\Database\Eloquent\Model;
use mysql_xdevapi\Exception;

/**
 * App\Models\MemberOrder
 *
 * @property int $id
 * @property int $order_no 订单号
 * @property int $member_id member_id
 * @property int $store_id store_id
 * @property float $discount 店铺折扣
 * @property float $money 支付金额
 * @property float $input_money 会员输入金额
 * @property float $coupon_money 卡券支付金额
 * @property string|null $payment_type 支付方式，微信|支付宝|充值余额
 * @property int $is_pay 1已支付，0未支付
 * @property string|null $pay_time 支付时间
 * @property string|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereCouponMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereDiscount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereIsPay($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereMemberId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereOrderNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder wherePayTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder wherePaymentType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereStoreId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberOrder whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class MemberOrder extends Model
{
    protected $table = 'member_order';

    protected $fillable = ["order_no", "member_id", "store_id", "discount", "money", "payment_type"];

    // 支付回调 分配比例
    public  function memberPayCommission($store_id, $money, $member_order_id, $member_id)
    {
        $store = Store::where('id', $store_id)->first();

        $store_income_proportion = Proportion::storeIncomeProportion($store_id); // 店铺收益比例
//        $inconme_money = floor($money * $store_income_proportion * 100) / 100;
        $inconme_money = bcmul($money, $store_income_proportion, 2);
        // todo 店铺收益
        $store->increment("total_money", $inconme_money);
        $store->increment("usable_money", $inconme_money);
        // todo 店铺收益日志
        $store_income_log = new StoreIncomeLog();
        $store_income_log->store_id = $store_id;
        $store_income_log->member_order_id = $member_order_id;
        $store_income_log->money = $inconme_money;
        $store_income_log->proportion = $store_income_proportion;
        $save = $store_income_log->save();
        if(!$save){
            throw new \Exception("店铺收益日志");
        }

        // todo 资金池收益
        $bonus_pool = BonusPool::where('id', 1)->first();

        $bonus_pool_proportion = Proportion::bonusPoolProportion($store_id); // 资金池收益比例

//        $bonus_pool_inconme_money = floor($money * $bonus_pool_proportion * 100) / 100;
        $bonus_pool_inconme_money = bcmul($money, $bonus_pool_proportion, 2);
        $bonus_pool->increment("total_money",$bonus_pool_inconme_money);
        $bonus_pool->increment("usable_money",$bonus_pool_inconme_money);
        // todo 资金池日志
        $bonus_pool_log = new BonusPoolLog();
        $bonus_pool_log->store_id = $store_id;
        $bonus_pool_log->member_order_id = $member_order_id;
        $bonus_pool_log->money = $bonus_pool_inconme_money;
        $bonus_pool_log->proportion = $bonus_pool_proportion;
        $save =$bonus_pool_log->save();
         if(!$save){
             throw new \Exception("资金池日志记录失败");
         };

        //todo  记录总消费记录奖金池队列列表
        $member_pay_count_money_queue = MemberPayCountMoneyQueue::where('member_id', $member_id)->first();
        if ($member_pay_count_money_queue) {
            $member_pay_count_money_queue->member_id = $member_id;
            $member_pay_count_money_queue->total_money += $money;
            $save = $member_pay_count_money_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额增加失败");
            }
        } else {
            $member_pay_count_money_queue = new MemberPayCountMoneyQueue();
            $member_pay_count_money_queue->member_id = $member_id;
            $member_pay_count_money_queue->total_money = $member_pay_count_money_queue->total_money + $money;
            $save = $member_pay_count_money_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额新增失败");
            }
        }

        $total_queue = MemberPayCountMoneyQueue::where('member_id', $member_id)->first();

        // todo 累计消费到达多少进入奖金池
        if ($total_queue->total_money > Constant::MEMBER_TOTAL_PAY_MONEY) {
            $num = floor($total_queue->total_money / Constant::MEMBER_TOTAL_PAY_MONEY);
            for ($i = 0; $i < $num; $i++) {
                // 写入奖金池
                $member_bonus_queue = new MemberBonusQueue();
                $member_bonus_queue->member_id = $member_id;
                $member_bonus_queue->status = 0;
                $member_bonus_queue->money = Constant::MEMBER_BONUS_MONEY;
                $save = $member_bonus_queue->save();
                if (!$save) {
                    throw new \Exception("奖金池写入失败");
                }
            }
            //写入奖金池后再减去总消费金额
            $total_queue->total_money = $total_queue->total_money - Constant::MEMBER_TOTAL_PAY_MONEY * $num;
            $save = $total_queue->save();
            if (!$save) {
                throw new \Exception("会员消费余额写入失败");
            }
        }

        // todo 支付成功增加订单总金额，消费总人数
        $member_order_count = MemberOrderCount::get();
        $member_order_count[0]->increment("money", $money); // 金额增加
        $member_order_count[0]->increment("num"); // 人数增加

        //todo 写入消息通知

        $notifyService = new NotifyService();
        $notifyService->createRemind($member_id, 'member', '2', 1, "-".$money);

    }



    /**
     * 会员现金和余额支付会调用这个接口
     * 会员消费总记录
     */
    public function memberBonusQueue($member_id)
    {
        $member_bonus_queue = new MemberBonusQueue();
        $member_bonus_queue->member_id = $member_id;
        $member_bonus_queue->status = 0;
        $member_bonus_queue->money = Constant::MEMBER_BONUS_MONEY;
        $save = $member_bonus_queue->save();
        if (!$save) {
            throw new \Exception("奖金池写入失败");
        }
    }

}
